<?php

declare(strict_types=1);

namespace App\Controller\Admin;

use App\Model\Admin\Roles;
use App\Model\User;
use App\Services\SmsService;
use Hyperf\Redis\Redis;
use Hyperf\HttpServer\Contract\RequestInterface;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface;
use Trait\ResponseMsg;
use App\Model\Normal;
use Hyperf\Di\Annotation\Inject;
use Phper666\JWTAuth\JWT;
use App\Services\Wechat;
use App\Controller\AbstractController;


class UsersController extends AbstractController
{
    use ResponseMsg;

    protected Redis $redis;
    protected Normal $normal;
    protected JWT $jwt;
    protected Wechat $wechat;

    #[Inject]
    protected User $user;
    #[Inject]
    protected Roles $roles;

    public function __construct()
    {
        $this->jwt = container()->get(JWT::class);
        $this->redis = container()->get(Redis::class);
        $this->normal = container()->get(Normal::class);
        $this->wechat = container()->get(Wechat::class);
        $this->smsService = container()->get(SmsService::class);
    }
    /**列表
     * @param RequestInterface $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function getList(RequestInterface $request):ResponseInterface
    {
//        try {
            $page = $request->inputs(['page', 'limit', 'keyword','is_vip','login_type']);
            $data['list']= $this->user->where(function ($query) use ($page) {
                if (array_key_exists('keyword', $page) && $page['keyword']) {
                    $query->where('nickname', 'like', '%' . $page['keyword'] . '%');
                    $query->orWhere('phone', 'like', '%' . $page['keyword'] . '%');
                }
                if (array_key_exists('login_type', $page) && $page['login_type']) {
                    $query->where('login_type',$page['login_type']);
                }
                if (array_key_exists('is_vip', $page) && $page['is_vip']) {
                    $query->where('is_vip',$page['is_vip']);
                }
            })->orderBy('id', 'desc')
                ->offset(($page['page'] - 1) * $page['limit'])
                ->limit($page['limit'])
                ->get()->toArray();
            $data['total'] = $this->user->where(function ($query) use ($page) {
                if (array_key_exists('keyword', $page) && $page['keyword']) {
                    $query->where('nickname', 'like', '%' . $page['keyword'] . '%');
                    $query->orWhere('phone', 'like', '%' . $page['keyword'] . '%');
                }
                if (array_key_exists('login_type', $page) && $page['login_type']) {
                    $query->where('login_type',$page['login_type']);
                }
                if (array_key_exists('is_vip', $page) && $page['is_vip']) {
                    $query->where('is_vip',$page['is_vip']);
                }
            })->count();
            return $this->success($data);
//        } catch (\Exception $e) {
//            return $this->error();
//        }
    }


    /**创建编辑
     * @param RequestInterface $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function create(RequestInterface $request):ResponseInterface
    {
        $data = $request->post();
        if (array_key_exists('id',$data) && $data['id'] > 0) {
            $id = $data['id'];
            if(array_key_exists('updated_at', $data)){
                unset($data['updated_at']);
            }
            unset($data['id']);
            if($data['vip_time']){
                $now = time();
                if(strtotime($data['vip_time']) > $now){
                    $data['is_vip'] = 1;
                }else{
                    $data['is_vip'] = 0;
                }
            }else{
                $data['is_vip'] = 0;
            }
            $this->user->where('id', $id)->update($data);
        }
        return $this->success();
    }

}
